/**
 * Created By: sisopipo Project Team
 * Created Date: Feb 25, 2011
 */
package com.sisopipo.assetsportal.security.impl;

import com.sisopipo.assetsportal.context.IAppSessionContext;
import com.sisopipo.assetsportal.security.IRoleManager;
import com.sisopipo.assetsportal.security.ISecurityService;
import com.sisopipo.assetsportal.security.domain.User;

/**
 * @author ribin
 * @version 1.0
 */
public class SecurityServiceImpl implements ISecurityService {

	private IAppSessionContext sessionContext;

	private IRoleManager roleManager;

	public boolean hasPermission() {
		String funcCode = sessionContext.getFuncCode();

		// if there isn't function code, then return false
		if (funcCode == null || funcCode.trim().length() == 0)
			return false;

		return hasPermission(funcCode);
	}

	public boolean hasPermission(String funcCode) {
		User curUser = getCurUser();
		if (curUser != null && curUser.getRole() != null && curUser.getRole().getId() != null)
			return roleManager.hasFunction(curUser.getRole().getId(), funcCode);
		return false;
	}

	private User getCurUser() {
		return sessionContext.getUser();
	}

	public void setSessionContext(IAppSessionContext sessionContext) {
		this.sessionContext = sessionContext;
	}

	public void setRoleManager(IRoleManager roleManager) {
		this.roleManager = roleManager;
	}

}
